package graph;

public class GraphUtil {
    public static Graph<Integer> makeGraph(int[][] matrix) {
        Graph<Integer> graph = new Graph<>();
        for (int i = 0; i < matrix.length; i++) {
            int weight = matrix[i][0];
            int from = matrix[i][1];
            int to = matrix[i][2];

            Node<Integer> fromNode = graph.nodeMap.computeIfAbsent(from, key -> new Node<>(key));
            Node<Integer> toNode = graph.nodeMap.computeIfAbsent(to, key -> new Node<>(key));
            Edge<Integer> edge = new Edge<>(weight, fromNode, toNode);

            fromNode.nextNodes.add(toNode);
            fromNode.out++;
            toNode.in++;
            fromNode.nextEdges.add(edge);
            graph.edges.add(edge);
        }
        return graph;
    }
}
